package cn.com.majiang.service.impl;

import cn.com.majiang.dao.AccountDao;
import cn.com.majiang.dao.ManagerDao;
import cn.com.majiang.domain.Account;
import cn.com.majiang.domain.Manager;
import cn.com.majiang.param.AccountParam;
import cn.com.majiang.service.IAccountService;
import cn.com.majiang.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangyanxin on 2017/5/28.
 */
@Service
public class AccountServiceImpl implements IAccountService{
    Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);
    @Autowired
    AccountDao accountDao;
    @Autowired
    ManagerDao managerDao;

    @Override
    public Map<String, Object> searchAccountList(AccountParam param) {
        int count = accountDao.selectAccountCount(param);
        if(param.getPage() != null) {
            param.setStartNum(param.getLimit() * (param.getPage() - 1));
        }
        List<Account> list = accountDao.selectAccountList(param);
        if(list == null || list.size() == 0){
            list = new ArrayList<>();
        }
        Map map = new HashMap();
        map.put("rows",list);
        map.put("count",count);
        map.put("onlineCount",0);
        if(StringUtils.isNotBlank(RedisUtil.getJedisValue("player.onlinenum"))){
            map.put("onlineCount", RedisUtil.getJedisValue("player.onlinenum"));
        }

        return map;
    }

    @Override
    public Boolean editAccountStatus(Account account) {
        return accountDao.updateAccountStatus(account) > 0;
    }

    @Override
    public Boolean updateRoomCard(Integer id, Manager manager, Integer payCardNum) {
        Account account = accountDao.selectByPrimaryKey(id);
        if(account == null) {
            logger.info("根据id没有找到玩家");
            return false;
        }
        account.setRoomcard(account.getRoomcard() + payCardNum);
        account.setTotalcard(account.getTotalcard() + payCardNum);
        try {
            if(manager.getPowerId() != 1){
                //1：不是超级管理元的情况下，检查mananger的房卡是否够充值
                if(manager.getActualcard() >= Math.abs(payCardNum)){
                    //修改两边的房卡
                    //TODO 总房卡数没减
                    manager.setActualcard(manager.getActualcard() - payCardNum);
                    //修改管理员/代理商
                    if(managerDao.updateActualcard(manager)>0){
                        int result = accountDao.updateRoomcard(account);
                        if(result > 0){
                            return true;
                        }
                    }
                }else{
                    logger.info("房卡数不足");
                    return false;
                }
            }
            else{
                //超管操作
                if(accountDao.updateRoomcard(account)>0){
                    logger.info("更新房卡数量成功");
                    return true;
                }
            }
        } catch (Exception e) {
            logger.error("充房卡操作异常"+e.getMessage());
        }
        return false;
    }

    @Override
    public Account getAccountById(Integer id) {
        return accountDao.selectByPrimaryKey(id);
    }

    @Override
    public Boolean updateIsManager(Account account) {
        return accountDao.updateIsManager(account) > 0;
    }
}
